/*
 * Class.cpp
 *
 *  Created on: Nov 19, 2010
 *      Author: dhovemey
 */

#include <cassert>
#include "runtime/SourceFile.h"
#include "runtime/Class.h"

Class::Class(ClassId clsId, unsigned flags, unsigned numFields)
	: m_clsId(clsId)
	, m_flags(flags)
	, m_numFields(numFields)
	, m_numSlots(numFields + ((flags & FLAG_MAGIC) ? 1 : 0))
	, m_objectSize(sizeof(Object) + m_numSlots*sizeof(Slot))
	, m_ast(0)
	, m_srcFile(0)
{
}

Class::~Class()
{
	if (m_srcFile) {
		m_srcFile->detach();
		if (m_srcFile->getRefCount() == 0) {
			delete m_srcFile;
		}
	}
}

void Class::setAST(ASTNode *ast)
{
	m_ast = ast;
}

ASTNode *Class::getAST()
{
	return m_ast;
}

void Class::setSourceFile(SourceFile *srcFile)
{
	assert(m_srcFile == 0);
	srcFile->attach();
	m_srcFile = srcFile;
}

SourceFile *Class::getSourceFile()
{
	return m_srcFile;
}

void Class::initialize(Object *obj) const
{
	obj->clsId = m_clsId;
	obj->flags = 0;
	unsigned numSlots = getNumSlots();
	for (unsigned i = 0; i < numSlots; i++) {
		obj->slots[i].p = 0;
	}
}
